function register() {
    api.openWin({
        name: 'userRegister',
        url: 'userRegister.html',
        opaque: true,
        vScrollBarEnabled: false
    });
}

function ensure() {
    
    var name = $api.byId('username').value;
    var pwd = $api.byId('password').value;
	
	if (name == "") {
		api.toast({
	        msg:'请输入手机号码'
        });
        
		return false;
	}
	if (pwd == "") {
		api.toast({
	        msg:'请输入密码'
        });
		
		return false;
	}
	
    var loginUrl = 'http://dagudunqianduan.com/auth/login';

    var bodyParam = {
        phone: name,
        password: pwd
    }
    
    api.showProgress({
        title: '正在登录...',
        modal: false
    });
    
    api.ajax({
    		method: 'post',
	    url: loginUrl,
	    cache: false,
	    timeout: 20,
	    headers: {
        		"Content-type" : "application/json;charset=UTF-8",
        		"Accept": "application/json",
        },
        data: {
        		body: JSON.stringify(bodyParam)
        }
    },function(ret,err) {
		if (ret.accessToken) {
        
			storeAccessToken(ret.accessToken);
			getUserInfo();
			        
            setTimeout(function () {
                api.closeWin();
                
                api.openWin({
	                name: 'index',
	                url: 'index.html'
                });
                
            }, 100);
                        
        } else {
        		// TODO
            api.alert({
                msg: err.msg
            });
        }
        
        api.hideProgress();
    });
    
}

// 登陆页面点击返回关闭app
function fnBackClose() {
	if (api.systemType == 'ios') {
		api.addEventListener({
	        name:'swiperight'
        },function(ret,err){
        		api.closeWidget({});
        });
	} else {
		api.addEventListener({
	        name:'keyback'
        },function(ret,err){
        		api.closeWidget({});
        });
	}
}

apiready = function () {
    var header = $api.byId('header');
    $api.fixStatusBar(header);
    
    // 监听返回事件
    fnBackClose();
};